package com.yunche.yunchebusiness.ui.activity.accountbook

import android.annotation.SuppressLint
import android.view.View
import com.bumptech.glide.Glide
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.base.rx.SchedulerUtils
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.AccountBookEntity
import com.yunche.yunchebusiness.network.RetrofitManager
import com.yunche.yunchebusiness.network.exception.ExceptionHandle
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_finance_detail.*


class FinanceDetailActivity : BaseActivity() {

    var id: Long = 0

    override fun setLayout(): Int = R.layout.activity_finance_detail

    override fun initView() {
        id = intent.getLongExtra("id", 0);
    }

    override fun initData() {
        getOrderDetail();
    }

    override fun initEvent() {

    }


    @SuppressLint("CheckResult")
    private fun getOrderDetail() {
        CommonDialog.showLoading(mContext!!, Constant.Loading)
        RetrofitManager.service.getFinanceDetail(id)
            .compose(SchedulerUtils.ioToMain())
            .subscribe({
                CommonDialog.cancle()
                setData(it.datas!!)
            }, {
                CommonDialog.cancle()
                ToastyUtils.showError(ExceptionHandle.handleException(it))
            })
    }

    private fun setData(detail: AccountBookEntity) {

        if(detail.type == 1){
            linlay_finance_head.visibility  = View.GONE
        }

        //remittance_status  打款状态(默认为1-打款中—-2-打款成功—-3—打款失败
       // tv_review tv_option_status
        // status 资金状态(-1 不需要进行审批操作 1.待审批状态 2.审批拒绝  3.审批通过

        tv_option_date_name.text = if (detail.type == 1) "入账时间：" else "提现时间："
        tv_option_date.text = detail.approverTime
        tv_option_status_name.text = if (detail.type == 1) "入账状态：" else "提现状态："
        //1审核中   2打回  3通过  -1 通过
        tv_review.text =
                if (detail.status == 1) "审核中" else if (detail.status == 2) "审批拒绝" else if (detail.status == 3 || detail.status == -1) "通过" else ""
        //1打款中  2打款成功  3打款失败
        tv_option_status.text = if (detail.remittanceStatus == 1) "打款中" else if (detail.remittanceStatus == 2) "打款成功" else if (detail.remittanceStatus == 3) "打款失败" else ""
        tv_print_date.text = detail.remittanceTime
        tv_option_money_name.text = if (detail.type == 1) "入账金额：" else "提现金额："
        if(detail.remittanceStatus == 3){
            tv_option_money.text = "0 元"
        }else{
            tv_option_money.text = detail.sum.toString() + "元"
        }

        tv_name.text = detail.accountName

        if (detail?.account != null) {
            var bankNum = ""
            var account = detail?.account
            var start = 0
            var index = account.length / 4
            for (i in 1..index) {
                bankNum += account.substring(start, 4 * i) + "  "
                start += 4
            }
            bankNum += account.substring(account.length - (account.length % 4), account.length)
            tv_card_no.text = bankNum
        }

        Glide.with(mContext!!).load(detail.bankIcon).into(iv_brand_cover)
    }

}
